﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;

namespace BulldogCMS.Core.Runtime.Modules
{
    public class BaseModuleController : System.Web.Mvc.Controller
    {
        private Assembly _currentAssembly = null;
         
        public void Init(Assembly currentAssembly)
        {
            this._currentAssembly = currentAssembly;
            Context.Request.Language = new System.Globalization.CultureInfo("cs-CZ"); // TODO resolving language system
        }

        protected ActionResult RedirectToModuleAction(string actionName, string controllerName)
        {
            if (Context.Request.CurrentModuleAreaProvider != null)
            {
                string providerPath = Context.Request.CurrentModuleAreaProvider.AreaName;
                string url = VirtualPathUtility.ToAbsolute(string.Format("~/{0}/{1}/{2}", providerPath, controllerName, actionName));
                return Redirect(url);
            }
            return HttpNotFound();
        }

        protected ActionResult ModuleView(string viewFileName, string masterPage = null, object model = null)
        {

            // ~/BulldogCMS.Manager/Views.Home.Index.cshtml

            string viewPath = string.Format("~/Views.{0}.cshtml", viewFileName);

            if (model != null)
            {
                if (masterPage != null)
                {
                    return View(viewPath, masterPage, model);
                }
                else
                {
                    return View(viewPath, model);
                }
            }
            else
            {
                if (masterPage != null)
                {
                    return View(viewPath, masterPage);
                }
                else
                {
                    return View(viewPath);
                }
            }
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
        }

    }
}